<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>The Ajax 02 Page</h1>
    <button onclick="doAjaxGet()">Do Ajax Get</button>
    <button onclick="doAjaxPost()">Do Ajax Post</button>
    <button onclick="doAjaxDelete()">Do Ajax Delete</button>
    <button onclick="doAjaxUpdate()">Do Ajax Update</button>
    <div id="result"></div>
    <script>
        function doAjaxGet() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState==4&&xhr.status==200){
                    console.log(xhr.responseText);
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("POST","http://localhost/doAjaxGet",true);
            //发送请求（post请求传递数据，需要将数据写入到send）
            xhr.send();
        }
        function doAjaxPost() {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState==4&&xhr.status==200){
                    console.log(xhr.responseText);
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("POST","http://localhost/doAjaxPost",true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            //发送请求（post请求传递数据，需要将数据写入到send）
            xhr.send("id=101&name=Computer&remark=Computer....");
        }
        function doAjaxDelete() {
            debugger;
            console.log(1);
            let xhr = new XMLHttpRequest();
            console.log(2);
            xhr.onreadystatechange = function () {
                console.log(1);
                if(xhr.readyState==4&&xhr.status==200){
                    console.log(xhr.responseText);
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("DELETE","http://localhost/doAjaxDelete?id=101",true);
            //发送请求（post请求传递数据，需要将数据写入到send）
            xhr.send(null);
        }
        function doAjaxUpdate() {

            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if(xhr.readyState==4&&xhr.status==200){
                    console.log(xhr.responseText);
                    document.getElementById("result").innerHTML=xhr.responseText;
                }
            }
            xhr.open("GET","http://localhost/doAjaxUpdate",true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
            //发送请求（post请求传递数据，需要将数据写入到send）
            xhr.send("id=101&remark=Cmp&name=cctv");
        }
    </script>
</body>
</html>